(function($){
	$.fn.extend({
		flying_table_head:function(opt){
			var i=1;
			return this.each(function() {
				var flying_table_head_table = $(this);
				var flying_table_head_tr = flying_table_head_table.find(".flying_table_head:first");
				flying_table_head_table.data("flying_table_head_id",i++);
				var flying_table_head_fixed=flying_table_head_table.flying_table_head_fixeder();
				if((flying_table_head_tr.size()>0) && (flying_table_head_fixed !=null)){
					$(window).scroll(function(){
						var a = $(document).scrollTop();
						var b = flying_table_head_tr.offset().top;
						var c = flying_table_head_table.offset().top+flying_table_head_table.outerHeight();
						var d = $(document).scrollLeft();
						var e = flying_table_head_fixed.outerHeight();
						flying_table_head_fixed.css("left",flying_table_head_tr.offset().left-d);
						if((a>b) && (a<c-e)){
							flying_table_head_fixed.show();
						}else{
							flying_table_head_fixed.hide();
						}
					}).resize(function(){
						flying_table_head_fixeder();
					});
				}
			});
		},
		flying_table_head_fixeder:function(){
			var flying_table_head_table = $(this);
			var flying_table_head_tr = flying_table_head_table.find(".flying_table_head");
			var flying_table_head_fixed= null;
			var i=flying_table_head_table.data("flying_table_head_id");
			var table_offset = flying_table_head_table.offset();
			var table_top = table_offset.top;
			var table_left = table_offset.left;
			var table_border = flying_table_head_table.attr("border");
			table_border = (table_border==undefined ||table_border.length==0)?0:parseInt(table_border);
			var cellspacing = flying_table_head_table.attr("cellspacing");
			cellspacing = (cellspacing==undefined ||cellspacing.length==0)?0:parseInt(cellspacing);
			var fid="flying_table_head_" + i;
			if($("#".fid).size()>0){
				$("#".fid).remove();
			}
			if(flying_table_head_tr.size()>0){
				var tr_height=0;
				flying_table_head_fixed = $("<div class='flying_table_head_div' id='flying_table_head_"+i+"'></div>").css({
						width:flying_table_head_tr.outerWidth(),
						position:'fixed',
						left:flying_table_head_tr.offset().left,
						display:'none',
						top:0
				}).appendTo("body");
				flying_table_head_tr.each(function(){
					tr_height+=$(this).outerHeight()+cellspacing;
					$(this).children().each(function(){
						var $this = $(this);
						var this_offset = $this.offset();
						var this_top = this_offset.top-table_top-table_border-1;
						var this_left = this_offset.left-table_left-table_border-1;

						$("<div class='flying_table_head_subdiv'></div>").css({
							"float":"left",
							"position":"absolute",
							"left":this_left+"px",
							"top":this_top+"px",
							"width":$this.innerWidth(),
							"height":$this.innerHeight()

						}).text($this.text()).appendTo(flying_table_head_fixed);

					});
				});
				flying_table_head_fixed.css("height",tr_height+cellspacing);
				if(cellspacing+table_border==1){
					flying_table_head_fixed.css({"border":"none"});
				}
			}
			return flying_table_head_fixed;
		}

	});
})(jQuery);
$(document).ready(function(){
	$("table").flying_table_head();
});
